---
title: "Figure A1 - Appendix"
author: "Jean-François Daoust and Thomas Gareau-Paquette"
output: html_document
---

```{r}
library(tidyverse)
library(ggthemes)
library(modelsummary)
library(scales)
```

```{r}
#setwd("/path/to/your/directory")
data <- read.csv("SES_pooled_cleaned.dta")
```

```{r}
df <- df %>%
    mutate(votec_pooled = as_factor(votec_pooled),
           votel_pooled = as_factor(votel_pooled),
           year_id = as_factor(year_id))
```

```{r}
df <- df %>% 
  pivot_longer(cols = c(votec_pooled, votel_pooled), names_to = "vote_type", values_to = "vote") %>%
  mutate(vote_type = ifelse(vote_type == "votec_pooled", "Constituency", "List")) %>%
    filter(vote != "Abstainer/DK") %>%
    filter(year_id != "2014")
```

```{r}
df <- df %>%
    mutate(vote = ifelse(vote == "Conservative", "Conservatives", vote))
```

```{r}
df <- df %>%
  group_by(year_id, vote_type) %>%  
  mutate(total = n()) %>% 
  group_by(year_id, vote_type, vote, total) %>% 
  summarise(count = n(), .groups = 'drop') %>%
  mutate(percentage = (count / total))
```

```{r}
df <- df %>%
    mutate(vote_type = factor(vote_type, levels = c("List", "Constituency")))

ggplot(df, aes(x = vote, y = percentage, fill = vote_type)) + # Multiply y by 100 if your data is in proportions
  geom_bar(stat = "identity", position = position_dodge(width = 0.7), width = 0.6) +
  scale_fill_manual(values = c("grey30", "grey70"), name = "Vote Type") +
  theme_bw() +
  labs(x = "Vote", y = "Percent", title = " ") +
  facet_wrap(vars(year_id)) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1),
        legend.position = "bottom") +
  scale_y_continuous(labels = percent_format(accuracy = 1)) 


ggsave("figureA1.png", width = 9, height = 5)
```
à